package net.hserver.admin.system.satoken;

import cn.dev33.satoken.stp.StpInterface;
import cn.hserver.core.ioc.annotation.Autowired;
import cn.hserver.core.ioc.annotation.Bean;
import lombok.RequiredArgsConstructor;
import net.hserver.admin.system.mapper.RoleMapper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import static net.hserver.admin.system.common.MyConstants.SUPER_ADMIN_ID;
import static net.hserver.admin.system.common.MyConstants.SUPER_ADMIN_PERM;


/**
 * 自定义权限验证接口扩展
 */
@Bean    // 保证此类被SpringBoot扫描，完成Sa-Token的自定义权限验证扩展
public class StpInterfaceImpl implements StpInterface {

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        long userId = Long.parseLong((String) loginId);
        if (userId == SUPER_ADMIN_ID) {
            return Collections.singletonList(SUPER_ADMIN_PERM);
        }
        Set<String> keys = roleMapper.listPermKey(userId);
        return new ArrayList<>(keys);
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        return Collections.emptyList();
    }

}
